package com.cxs.distributedgamedemo.slice;

import com.cxs.distributedgamedemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Text;
import ohos.bundle.ElementName;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(c -> {
            // 获取当前设备信息
            ohos.data.distributed.device.DeviceInfo localDeviceInfo = KvManagerFactory.getInstance()
                    .createKvManager(new KvManagerConfig(this)).getLocalDeviceInfo();
            // 当前设备Id
            String localDeviceId = localDeviceInfo.getId();
            // 当前设备Type
            int localDeviceType = Integer.parseInt(localDeviceInfo.getType());
            if (localDeviceType == DeviceInfo.DeviceType.SMART_PHONE.getVal()) {
                // 如果当前设备是手机,跳转到GamepadAbility
                startFA("", "GamepadAbility");
            } else if(localDeviceType == DeviceInfo.DeviceType.SMART_PAD.getVal()) {
                // 如果当前设备是pad,跳转到GameUIAbility
                startFA("", "GameUIAbility");
            }

            /*
            获取所有设备，判断目标设备
                手机：  跳转到GamepadAbility
                pad：  跳转到GameUIAbility
            */
            List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ALL_DEVICE);
            if (deviceList != null) {
                for (DeviceInfo deviceInfo : deviceList) {

                    DeviceInfo.DeviceType deviceType = deviceInfo.getDeviceType();
                    String deviceId = deviceInfo.getDeviceId();

                    if (deviceType == DeviceInfo.DeviceType.SMART_PHONE) {
                        // 远程设备跳转到GamepadAbility
                        startFA(deviceId, "GamepadAbility");
                    } else if (deviceType == DeviceInfo.DeviceType.SMART_PAD){
                        // 远程设备跳转到GameUIAbility
                        startFA(deviceId, "GameUIAbility");
                    }
                }
            }


        });

    }
    // 打开本设备/远程设备FA
    public void startFA(String deviceId, String abilityName) {
        Intent intent = new Intent();

        if (deviceId == "") {
            // 本地跳转
            ElementName elementName = new ElementName("", "com.cxs.distributedgamedemo", abilityName);
            intent.setElement(elementName);
        } else {
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId(deviceId)
                    .withBundleName("com.cxs.distributedgamedemo")
                    .withAbilityName(abilityName)
                    .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                    .build();
            intent.setOperation(operation);
        }

        startAbility(intent);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
